<?php

/**
 * uniBoard Advanced sorting class
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date$
 * $Revision: 35 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class class_hierarchy{
	
	/**
	 * Raw, unordered array
	 *
	 * @var array
	 */
	
	private static $raw_array = array();
	
	/**
	 * Completed array
	 *
	 * @var array
	 */
	
	private static $finished_array 		= array();
	
	/**
	 * Use images?
	 *
	 * @var bool
	 */
	
	private static $use_images 			= false;
	
	/**
	 * Images array
	 *
	 * @var array
	 */
	
	private static $images 				= array();
	
	// ------------------------------------------------------------------------
	
	/**
	 * Orders list to n-depth
	 *
	 * @param array $array_to_order
	 * @return array
	 */
	
	static function doOrdering( $array_to_order, $images = array()){
		
		// Store new array
		self::$raw_array = (array) $array_to_order;
		
		// Tree images
		self::$images = $images;
		
		// Use images?
		self::$use_images = !empty( $images);
		
		// Clear finished array
		self::$finished_array = array();
		
		// Run recurrention
		if ( isset( self::$raw_array[0]))
		{
			self::recurreOrder( 0, -1, '');
		}
		
		// Array to return
		$ready_array = self::$finished_array;
		
		// Clear finished array
		self::$finished_array = array();
		
		// Return complete array
		return $ready_array;
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Recurring function for ordering
	 *
	 * @param mixed $parent_id
	 * @param integer $tree_depth
	 */
	
	private static function recurreOrder( $parent_id, $tree_depth = 0, $depth_image){
						
		// Item childs number
		$item_childs = count( self::$raw_array[$parent_id]);
		
		// Actual child number
		$actual_child = 0;
		
		// Increase depth
		$tree_depth ++;
		
		// Iterate
		foreach ( self::$raw_array[$parent_id] as $array_item_id => $array_item_data)
		{
			// Increase child number
			$actual_child ++;
			
			// Build depth of item
			$array_item_data['item_depth'] = $tree_depth;
			
			// Draw images?
			if ( self::$use_images && $tree_depth > 0)
			{
				// Last item?
				if ( $actual_child == $item_childs)
				{
					// Next image
					$next_img = $depth_image . self::$images[0];
				}
				else
				{
					// Next image
					$next_img = $depth_image . self::$images[1];
				}
				
				// Set image
				$array_item_data['item_image'] = $depth_image;
				
				// Last item?
				if ( $actual_child == $item_childs)
				{
					// Add "L" turn
					$array_item_data['item_image'] .= self::$images[3] . ' ';
				}
				else
				{
					// Add "T" turn
					$array_item_data['item_image'] .= self::$images[2] . ' ';
				}
				
			}
			else
			{
				// Next image
				$next_img = '';
				
				// Empty string
				$array_item_data['item_image'] = '';
				
			}
						
			// Add our item to list
			self::$finished_array[$array_item_id] = $array_item_data;
			
			// Recure?
			if ( isset( self::$raw_array[$array_item_id]))
			{
				self::recurreOrder( $array_item_id, $tree_depth, $next_img);
			}
		}
				
	}
	
}